import common from './common.js'

class StorageUtils {
	public setStorageSync(key: string, data: any, callback?: Function) {
		try {
			uni.setStorageSync(key, data)
			callback && callback(2001)
		} catch (e) {
			callback && callback(2000)
		}
	}

	/** 同步,从本地缓存中同步获取指定 key 对应的内容 **/
	public getStorageSync(key: string) {
		const value = uni.getStorageSync(key);
		const data = value ? common.stringToJson(value) : null
		return data;
	}

	public async setStorage(key: string, data: any) {
		uni.setStorageSync(key, data)
		return Promise.resolve()
	}

	public async getStorage(key: string) {
		try {
			const value = uni.getStorageSync(key);
			const data = value ? common.stringToJson(value) : null
			return Promise.resolve(data)
		}
		catch (e) {
			return Promise.reject(e)
		}
	}
}

export default new StorageUtils();
